package com.example.demo1122.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo1122.DTO.StudentDTO;
import com.example.demo1122.domain.ResponseResult;
import com.example.demo1122.entity.Chapter;
import com.example.demo1122.DTO.ChapterDTO;
import com.example.demo1122.entity.Class;
import com.example.demo1122.entity.Student;
import com.example.demo1122.mapper.ChapterMapper;
import com.example.demo1122.service.IChapterService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements IChapterService {
    @Resource
    private ChapterMapper chapterMapper;

    @Override
    public ResponseResult showChapter(Integer classID) {
        try {
            List<Chapter> chapterList = chapterMapper.showChapter(classID);
            return new ResponseResult(1, "success", chapterList);
        } catch (Exception e) {
            return new ResponseResult(0, "error", e);
        }
    }

    @Override
    public ResponseResult saveChapter(List<ChapterDTO> chapterDTOs, Integer courseID) {
        List<String> duplicateChapterNames = new ArrayList<>();
        List<Integer> duplicateChapterNums = new ArrayList<>();
        Set<String> chapterNames = new HashSet<>();
        Set<Integer> chapterNums = new HashSet<>();

        for (ChapterDTO chapterDTO : chapterDTOs) {
            if (!chapterNums.add(chapterDTO.getChapterNum())) {
                duplicateChapterNums.add(chapterDTO.getChapterNum());
            }
            else if (!chapterNames.add(chapterDTO.getChapterName())) {
                duplicateChapterNames.add(chapterDTO.getChapterName());
            }
            else {
                Chapter chaptertemp = new Chapter();
                chaptertemp.setChapterName(chapterDTO.getChapterName());
                chaptertemp.setChapterNum(chapterDTO.getChapterNum());
                chaptertemp.setCourseID(courseID);
                chapterMapper.insert(chaptertemp);
            }

        }

        if ((!duplicateChapterNums.isEmpty())||(!duplicateChapterNames.isEmpty())) {
            if(!duplicateChapterNums.isEmpty()){
                if (!duplicateChapterNames.isEmpty()){
                    return new ResponseResult(4271, "本课程中以下章节编号重复: " +duplicateChapterNums +";以下章节名称重复:" + duplicateChapterNames);
                }
                else {
                    return new ResponseResult(4272, "本课程中以下章节编号重复: " +duplicateChapterNums);
                }
            }
            else {
                return new ResponseResult(4273, "本课程中以下章节名称重复:" + duplicateChapterNames);
            }
        }
        else {
            return new ResponseResult(4270, "添加成功");
        }
    }


    @Override
    public ResponseResult findChapterByCourseID(Integer courseID) {
        // 首先检查classID是否存在

            // 调用ClassStuMapper的方法来获取所有的studentID
            List<Integer> chapterIds = chapterMapper.selectChapterIdsByCourseId(courseID);
            List<Chapter> chapters=chapterMapper.selectBatchIds(chapterIds);
            if (chapters.isEmpty()) {
                return new ResponseResult<>(2135, "该课程中没有章节", Collections.emptyList());
            } else {

                return new ResponseResult<>(HttpStatus.OK.value(), "查询成功", chapters);
            }

    }



    @Override
    public ResponseResult insertOrupdateChapter(Chapter chapter) {
        if (chapter.getChapterID() != null){
            if (chapterMapper.updateChapter(chapter) != 0) {
                return new ResponseResult<>(1, "更改章节信息成功");
            }
            else return new ResponseResult<>(0, "更改章节信息失败");
        }
        else {
            if (chapterMapper.insertChapter(chapter) != 0){
                return new ResponseResult<>(1, "新增章节信息成功");
            }
            else return new ResponseResult<>(0, "新增章节信息失败");
        }
    }
}
